import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../theme/color_palettes.dart';

SliverAppBar commonSliverAppBar(
    {String? title,
    Widget? leading,
    List<Widget>? action,
    GestureTapCallback? leadingClick,
    List<GestureTapCallback>? actionClick}) {
  return SliverAppBar(
    title: Text(title ?? "",
        style: TextStyle(
            color: ColorPalettes.instance.firstText,
            fontSize: 36.w,
            fontWeight: FontWeight.w500)),
    centerTitle: true,
    pinned: true,
    actions: action != null && actionClick != null
        ? handleAction(action: action, actionClick: actionClick)
        : action,
    leading: leadingClick != null
        ? GestureDetector(
            onTap: leadingClick,
            child: leading,
          )
        : leading,
    backgroundColor: Colors.transparent,
    flexibleSpace: const FlexibleSpaceBar(
        background: Stack(
      fit: StackFit.expand,
      children: <Widget>[
        DecoratedBox(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.bottomLeft,
              end: Alignment.topRight,
              colors: [Colors.red, Colors.blue], // 渐变颜色
            ),
          ),
        ),
      ],
    )),
  );
}

List<Widget> handleAction(
    {required List<Widget> action,
    required List<GestureTapCallback> actionClick}) {
  List<Widget> actionWidget = [];
  for (int i = 0; i < action.length; i++) {
    actionWidget.add(
      Padding(
        padding: EdgeInsets.only(right: 50.w), // 添加右侧间距
        child: GestureDetector(
          onTap: actionClick[i],
          child: action[i],
        ),
      ),
    );
  }
  return actionWidget;
}
